﻿using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using Quadrant.domain.Models;
using Quadrant.domain.Services;
using Quadrant.web.Helpers;

namespace Quadrant.web.API
{
    [Authorize]
    public class ZonesController : BaseApiController
    {
        [ResponseType(typeof(Zone[]))]
        public async Task<IHttpActionResult> Get()
        {
            using (var srv = new ZoneServices(this.Logger))
            {
                var lst = await srv.GetZones(this.UserId);
                return Ok(lst);
            }
        }

        [ResponseType(typeof(Zone))]
        public async Task<IHttpActionResult> Get(int id)
        {
            using (var srv = new ZoneServices(this.Logger))
            {
                return Ok(await srv.GetZone(id));
            }
        }

        [ResponseType(typeof(void))]
        public async Task<IHttpActionResult> Post(Zone zone)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            using (var srv = new ZoneServices(this.Logger))
            {
                var save = await srv.SaveZone(zone);
                return Ok(save);
            }

        }
    }
}
